from pymongo import MongoClient
from pyquery import PyQuery as pq

head = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.48'
}


def getbookslists(url):
    tlist = []
    doc = pq(url=url,headers=head)
    mylist = doc("table")
    for web in mylist.items():
        leidict={
            "书名": web(".pl2").text().replace("\n", "   ").replace("'","‘"),
            "简介": web('p[class^="pl"]').text(),
            "评分": web('span[class^="rating_nums"]').text(),
            "评价人数": web('span[class^="pl"]').text()[2:11],
            "引用语": web(".inq").text()
        }
        tlist.append(leidict)
    return tlist


def cunchu(weilist):
    client = MongoClient("mongodb://lwjbigdata:123456@localhost:27017/?authMechanism=DEFAULT&authSource=admin")
    collection = client["spiderwork"]["豆瓣图书"]
    try:
        print("正在写入数据！")
        collection.insert_many(weilist)
    except:
        print("数据写入失败！")


for i in range(10):
    url = f"https://book.douban.com/top250?start={25 * i}"
    weilist = getbookslists(url)
    cunchu(weilist)
